package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.modles.CommentsListData;

import java.util.List;

/**
 * 二级页面-->小唐君有图页面（评论 适配器）
 * Created by lanouhn on 16/9/3.
 */
public class ACTSifeSecondReviewAdapter extends RecyclerView.Adapter<ACTSifeSecondReviewAdapter.ReviewViewHolder> {


    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private Context context;
    private CommentsListData commentsListData;

    public ACTSifeSecondReviewAdapter(Context context, CommentsListData commentsListData) {
        this.context = context;
        this.commentsListData = commentsListData;
    }

    @Override
    public ReviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.actsifesecond_two_item_review, null);
        ReviewViewHolder holder = new ReviewViewHolder(view);


        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(final ReviewViewHolder holder, final int position) {

        final List<CommentsListData.DataBean> dataBean = commentsListData.getData();

        if (holder.linearLayout_two_item_review_content.getChildCount() > 0)
            holder.linearLayout_two_item_review_content.removeAllViews();

        if (dataBean.size() > 0) {
            holder.linearLayout_review_view.setVisibility(View.VISIBLE);
            if (position == 0) {
                holder.linearLayout_review_text.setVisibility(View.VISIBLE);
            } else {


                int i = position - 1;
                holder.linearLayout_review_content.setVisibility(View.VISIBLE);
                Picasso.with(context).load(dataBean.get(i).getUser().getAvatar()).into(holder.review_logo);
                holder.review_name.setText(dataBean.get(i).getUser().getNickname());
                holder.review_time.setText(dataBean.get(i).getDatestr());

                //if (commentsListData.getData().get(i).getAt_user().equals("")) {

                    TextView textView1 = new TextView(context);
                    textView1.setText(dataBean.get(i).getConent());
                    holder.linearLayout_two_item_review_content.addView(textView1);

               // }
//                if (!commentsListData.getData().get(i).getAt_user().equals("")) {
//                    TextView textView2 = new TextView(context);
//
//                    String string = "回复 @" + dataBean.get(i).getAt_user().getNickname() + "：" + dataBean.get(i).getConent();
//                    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
//                    spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.mycolor)),
//                            3, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                    textView2.setText(spannableStringBuilder);
//                    holder.linearLayout_two_item_review_content.addView(textView2);
//                }

            }
        }

        //点击 评论区 的条目，弹出Dialog
        holder.linearLayout_review_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder aBuilder=new AlertDialog.Builder(context);
                View view = LayoutInflater.from(context).inflate(R.layout.comments_dialog, null);
                aBuilder.setView(view);
                aBuilder.setCancelable(false);
                final AlertDialog alertDialog = aBuilder.create();

                TextView reply = (TextView) view.findViewById(R.id.reply_comments_dialog);//回复
                TextView copy = (TextView) view.findViewById(R.id.copy_comments_dialog);//复制
                TextView callof = (TextView) view.findViewById(R.id.callof_comments_dialog);//取消
                TextView report= (TextView) view.findViewById(R.id.report_comments_dialog);//举报
                View reportView=view.findViewById(R.id.reportView_comments_dialog);//举报下的线

                report.setVisibility(View.VISIBLE);
                reportView.setVisibility(View.VISIBLE);

                reply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                     alertDialog.cancel();
                    }
                });
                copy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.cancel();
                    }
                });
                callof.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.cancel();
                    }
                });
                report.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.cancel();
                    }
                });
            }
        });

        //点击 作者名字或头像，进入作者介绍页面
        holder.review_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String author_ID = dataBean.get(position-1).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

        //点击 作者名字或头像，进入作者介绍页面
        holder.review_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = dataBean.get(position-1).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return commentsListData.getData().size() + 1;
    }


    public class ReviewViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout linearLayout_review_view, linearLayout_review_text,
                linearLayout_review_content, linearLayout_two_item_review_content;
        private TextView review_name, review_time, review_content;
        private ImageView review_logo;

        public ReviewViewHolder(View itemView) {
            super(itemView);
            linearLayout_review_view = (LinearLayout) itemView.findViewById(R.id.linearLayout_review_view);
            linearLayout_review_text = (LinearLayout) itemView.findViewById(R.id.linearLayout_review_text);
            linearLayout_review_content = (LinearLayout) itemView.findViewById(R.id.linearLayout_review_content);
            linearLayout_two_item_review_content = (LinearLayout) itemView.findViewById(R.id.linearLayout_two_item_review_content);

            review_name = (TextView) itemView.findViewById(R.id.two_item_review_name);
            review_time = (TextView) itemView.findViewById(R.id.two_item_review_time);
            //review_content = (TextView) itemView.findViewById(R.id.two_item_review_content);

            review_logo = (ImageView) itemView.findViewById(R.id.two_item_review_logo);
        }
    }
}
